﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;


using System.Text.RegularExpressions;
using System.IO;

/// <summary>
///makehtml 的摘要说明
/// </summary>
public class makehtml
{
    /// <summary>
    /// 写html
    /// </summary>
    /// <param name="Content"></param>
    /// <param name="FilePath"></param>
    public void WriteHtml(string Content, string FilePath)
    {
        string getContent = "";
        string getajaxJS = "";
        string byCreat = "<!--Created by BMCCMS v1.0 at " + DateTime.Now + "-->\r\n";
        try
        {
            string Dir = FilePath.Substring(0, FilePath.LastIndexOf("\\"));
            if (!Directory.Exists(Dir))
                Directory.CreateDirectory(Dir);
            using (StreamWriter sw = new StreamWriter(FilePath, false))
            {
                if (Regex.Match(Content, @"\</head\>[\s\S]*\<body", RegexOptions.IgnoreCase | RegexOptions.Compiled).Success)
                {
                    getContent = Regex.Replace(Content, "<body", getajaxJS + byCreat + "<body", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                }
                else
                {
                    getContent = getajaxJS + byCreat + Content;
                }
                sw.Write(getContent);
                sw.Dispose();
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p style=\"font-size:12px\">系统提示出错信息：写入文件出错，" + ex.Message + "<a href=\"javascript:history.back()\" title=\"返回\">返回</a><p>");
            HttpContext.Current.Response.Write("<p style=\"font-size:12px\"><b>可能原因是：</b><ul style=\"list-style:square;font-size:12px\"><li>网站目录没有设置读写权限；</li><li>网站目录没有相应用户的操作权限（如NETWORK SERVICE）</li></p>");
            HttpContext.Current.Response.End();
        }
    }

    /// <summary>
    /// 生成详细页
    /// </summary>
    /// <param name="detailid"></param>
    /// <param name="tempath"></param>
    /// <param name="htmlpath"></param>
    public string makeDetail(string detailid, string tempath, string htmlpath, string filename)
    {
        string str = string.Empty;
        Template tem = new Template(tempath, TempType.News);
        tem.NewsID = detailid;
        tem.ReplaceLabels();
        tem.ReplaceIMLabels();
        string fileNameEx = ".html";
        int pos1 = tem.FinallyContent.IndexOf("[page]");
        int pos2 = tem.FinallyContent.IndexOf("[/page]");
        string allContent = tem.FinallyContent;
        filename = "t" + detailid;
        if (tem.FinallyContent.IndexOf("[BMC:cPage]") > 0)
        {
            string content = tem.FinallyContent;
            string head = content.Substring(0, pos1);
            string end = content.Substring(pos2 + 7);
            string mid = content.Substring(pos1 + 6, pos2 - pos1 - 6);

            string[] strArry = Regex.Split(mid, @"\[BMC:cPage\]", RegexOptions.IgnoreCase);
            for (int i = 0; i < strArry.Length; i++)
            {
                if (strArry[i].ToString().Trim() != "" && strArry[i] != null && strArry[i].ToString().Trim() != string.Empty)
                {
                   
                    string Acontent = head + strArry[i]  + end;
                    string ff = string.Empty;
                    if (i == 0)
                    {
                        ff = htmlpath + filename + fileNameEx;
                    }
                    else
                    {
                      
                        ff = htmlpath + filename + "_" + (i + 1) + fileNameEx;
                    }
                    WriteHtml(Acontent, ff);

                }

            }
        }
        else
        {
            WriteHtml(allContent, htmlpath + filename + fileNameEx);
        }
        return filename + fileNameEx;
    }

}